
package view;

import controller.VendaIngressoController;
import javax.swing.JOptionPane;
import model.MudaBanner;
import model.SessoesModel;

/**
 * @author Lucas Amaral
 */
public class VendaIngressoView extends javax.swing.JFrame {
    private VendaIngressoController controller;
    private SessoesModel model;
    
    public void setBanner(String nomeBanner){
        this.jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource(nomeBanner)));
    }
    
    public VendaIngressoView(SessoesModel m) {
        initComponents();
        model = m;
        controller = new VendaIngressoController(this, model);
        controller.listarFilmes(true);
        MudaBanner mudabanner = new MudaBanner();
        mudabanner.setView(this);
        Thread threadBanner = new Thread(mudabanner);
        threadBanner.start();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelCabecalho = new javax.swing.JLabel();
        jLabelFilme = new javax.swing.JLabel();
        jComboBoxFilme = new javax.swing.JComboBox();
        jScrollPane = new javax.swing.JScrollPane();
        jTableTabelaResultado = new javax.swing.JTable();
        jButtonConfirmarCompra = new javax.swing.JButton();
        jLabelQuantidade = new javax.swing.JLabel();
        jComboBoxQuantidade = new javax.swing.JComboBox();
        jButtonSair = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabelCabecalho.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabelCabecalho.setText("Venda ingresso");

        jLabelFilme.setText("Filme");

        jComboBoxFilme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxFilmeActionPerformed(evt);
            }
        });

        jTableTabelaResultado.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane.setViewportView(jTableTabelaResultado);

        jButtonConfirmarCompra.setText("Confirmar compra");
        jButtonConfirmarCompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmarCompraActionPerformed(evt);
            }
        });

        jLabelQuantidade.setText("Quantidade");

        jComboBoxQuantidade.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", " " }));

        jButtonSair.setText("Sair");
        jButtonSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSairActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/img1.jpeg"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelFilme)
                                .addGap(6, 6, 6)
                                .addComponent(jComboBoxFilme, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabelQuantidade)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBoxQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelCabecalho)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonConfirmarCompra)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonSair)
                        .addGap(19, 19, 19))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelCabecalho)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxFilme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelFilme)
                    .addComponent(jLabelQuantidade)
                    .addComponent(jComboBoxQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonConfirmarCompra)
                    .addComponent(jButtonSair))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBoxFilmeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxFilmeActionPerformed
        if (jComboBoxFilme.getSelectedItem().toString() != null && !jComboBoxFilme.getSelectedItem().toString().equals("")) {
            controller.consultarFilmesSessoes(jComboBoxFilme.getSelectedItem().toString());
        }
    }//GEN-LAST:event_jComboBoxFilmeActionPerformed

    private void jButtonConfirmarCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfirmarCompraActionPerformed
        try {
            JOptionPane.showMessageDialog(this, controller.venderIngressoSessao((Integer) jTableTabelaResultado.getValueAt(jTableTabelaResultado.getSelectedRow(), 0), jComboBoxQuantidade.getSelectedItem().toString()));
        } catch(ArrayIndexOutOfBoundsException e) {
            JOptionPane.showMessageDialog(this, "Por favor, selecione a linha com a sessão desejada");        
        }
    }//GEN-LAST:event_jButtonConfirmarCompraActionPerformed

    private void jToggleButtonSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButtonSairActionPerformed
        
    }//GEN-LAST:event_jToggleButtonSairActionPerformed

    private void jButtonSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSairActionPerformed
        dispose();
    }//GEN-LAST:event_jButtonSairActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VendaIngressoView(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConfirmarCompra;
    private javax.swing.JButton jButtonSair;
    public javax.swing.JComboBox jComboBoxFilme;
    private javax.swing.JComboBox jComboBoxQuantidade;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelCabecalho;
    private javax.swing.JLabel jLabelFilme;
    private javax.swing.JLabel jLabelQuantidade;
    private javax.swing.JScrollPane jScrollPane;
    public javax.swing.JTable jTableTabelaResultado;
    // End of variables declaration//GEN-END:variables
}
